#include "casual_includes.h"
#include "acquisition.h"
#include "execution.h"
#include "interboursier.h"
#include "terminal.h"
#include "../libbourse/alea.h"
#include "../libcomm/communication.h"

int main(int argc, char** argv) {
	int num_term=2, num_ack=2;
	if (argc==3) {
		num_term=atoi(argv[1]);
		num_ack=atoi(argv[2]);
	}
	printf(
			"Lancement du programme avec %d terminaux (premier arg) et %d serveurs (second arg) d'acquisition/exécution\n",
			num_term, num_ack);
	init_log("bourse.log");
	int cur_bourse=0,i,cur_port=PORT_INTERBOURSIER;

	module *inter=init_module("Interboursier", interb, cur_bourse++);
	launch_interboursier(inter, cur_port++, num_ack);
	sleep(3); // on attend 3sec avant de lancer Acquisition
	for(i=0;i<num_ack;i++){
		char tmp[56];
		sprintf(tmp,"Acquisition 0%d",i);
		module *sack=init_module(tmp, ack, cur_bourse);
		launch_acquisition(sack, cur_port++);
		sprintf(tmp,"Execution 0%d",i);
		module *exect=init_module(tmp, exec, cur_bourse);
		launch_execution(exect, cur_port++);
		cur_bourse++;
	}
	cur_port=PORT_INTERBOURSIER+1; // les terminaux se connectent aux serveur d'acquisition
	for(i=0;i<num_term;i++){
		char tmp[56];
		sprintf(tmp,"Terminal 0%d",i);
		module *term1=init_module(tmp, term, 0);
		launch_terminal(term1, cur_port);
		cur_port+=2;
	}
	while(1);
	sleep(100); // oblige l'utilisateur à faire ctrl+c pour terminer le programme sinon erreur de bind ... :s
	printf("fin.\n");
	return 0;
}
